package temp;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class Test2 {

	public static void main(String[] args) throws IOException {

		//		File file = new File("E:\\temp\\a.txt");
		String fileName = "a.txt";
		String data = "";
		if (args != null) {
			if (args.length > 0) {
				fileName = args[0];
			}
			if (args.length > 1) {
				data = args[1];
			}
		}
		FileOutputStream outputStream = new FileOutputStream(fileName);
		printText(data, false);
		Scanner scanner = new Scanner(System.in);
		StringBuilder result = new StringBuilder();
		boolean run = true;
		while (run) {
			String next = scanner.nextLine();
			if (next != null) {
				if (next.equals("exit") || next.equals("finish")) {
					run = false;
				} else if (next.equals("flush")) {
					outputStream.flush();
				} else {
//					outputStream.write(next.getBytes());
//					System.out.println(next);
					result.append(next).append("\r\n");
				}
			} else {
				run = false;
			}

		}
		outputStream.write(result.toString().getBytes("utf-8"));
		outputStream.flush();
		outputStream.close();
	}

	public static void TestMethod() {
		String aText = "aaaaaaaaaaa";
		printText(aText, false);
	}

	public static String printText(String aText, boolean isPrintln) {
		String ln = "/r/n";
		System.out.println(aText);
		return isPrintln ? aText + ln : aText;
	}

}
